package sdk.webview.fmc.com.fmcsdk.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import sdk.webview.fmc.com.fmcsdk.R;

/**
 * Created by ydm on 2017/12/29.
 * 上传图片的dialog
 */

public class UploadIamgeDialog extends Dialog{
    private Context context;
    private int textViewVisible;
    private String msg = "";
    public UploadIamgeDialog(Context context) {
        super(context, R.style.loadingDialogStyle);
        this.context = context;
        textViewVisible = View.GONE;
    }

    public UploadIamgeDialog(Context context, String msg){
        super(context, R.style.loadingDialogStyle);
        this.msg = msg;
        this.context = context;
        textViewVisible = View.VISIBLE;
    }
    public UploadIamgeDialog(Context context, int theme){
        super(context,theme);
        this.context = context;
        textViewVisible = View.GONE;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.upload_iamge_dialog);
        initView();
    }

    private void initView(){
        TextView textView = (TextView) findViewById(R.id.tv);
        GifView loading_gifview = (GifView) findViewById(R.id.loading_gifview);
        loading_gifview.setGifResource(R.drawable.loading);
        textView.setText(msg);
        textView.setVisibility(textViewVisible);
        setCanceledOnTouchOutside(true);
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.alpha=0.9f;
        getWindow().setAttributes(attributes);
        setCancelable(false);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode){
            case KeyEvent.KEYCODE_BACK:
                if(UploadIamgeDialog.this.isShowing())
                    UploadIamgeDialog.this.dismiss();
                break;
        }
        return true;
    }

    public void setText(String text){
        this.msg = text;
        textViewVisible = View.VISIBLE;
    }

    public void setText(int resource){
        this.msg = context.getString(resource);
        textViewVisible = View.VISIBLE;
    }

    public void setMessage(String text){
        setText(text);
    }

    public void setMessage(int resource){
        setText(resource);
    }

    public void setTextVisibility(int visibility){
        if ((View.VISIBLE == visibility) || (visibility == View.INVISIBLE) || (View.GONE == visibility))
            textViewVisible = visibility;
    }
}
